/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
/*
 * Generator.java
 *
 * Created on January 8, 2003, 3:59 PM
 */

package tests.jd.crypt;

import javax.servlet.*;
import javax.servlet.http.*;

import java.text.*;
import java.util.*;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import java.math.*;
import sun.misc.BASE64Encoder;
import java.sql.*;

/**
 *
 * @author  jdepons
 * @version
 */
public class Generator extends HttpServlet {
   
    private String dbURL;
    private String dbDriver;
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        dbURL = config.getInitParameter("dbURL");
        dbDriver = config.getInitParameter("dbDriver");
    }
        
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        try {
            /*
            PublicExponent=10001
            modulus=96142334799ba0417560828127f1fef7682059b107f828dfbc953cf5840f5d02433303f43f0743bb253a7ac5539ef9c1e9cc1038c3210315d331c8593d977ce70d9790ba98e565fe5158166dc1b36ae6587d8b8c4fded7687cf2e2983dcc98f81b6e79c11282fe65697b6f52844a9700331a151283681a726f85b8483f012e7e32040a4d0d7e6f6d21008afc5263ddd3ab638b7e6d74f68614752cb3a06f1d5dd416b7a46497e082a7da21e4b151edfbb69e1a9bfb7a77e2ffc048e5df76fb4d6855a2ca8a9865ecf8bcd47848fd104f67f91581151ef59b1f9483bdd2d2596484ba3c10ab679fe4c37b41d96a239ed4316079969beec783714e0aba8bdc96a5
            PrivateExponent=874c924c7d91c2d1208ba83bde1239e53bd76664c625805bec7cb29f38b64d087c22a2def06f912d47abc800db9947dafcf6be6d1de5460dc7ff6a7aa19f4fb851b5a181c7d38fb6ce08d6f78e183d387af07bfd3c084f3460e8eb458af947b2edb0144dee6d7001672e23b74592a53d45d4aec8a8f5125ab695dc042de0d7d30257d2c048218daceba0b81c80be1532bcf4010013850ae23c59f53f2fd944cb5350ac5cc9e929928f649446a08221a77d6e7ca24ae5c05de0b2569877bf5e72cc8bdb075cb4baad9395f500db61b97c699fce815f391aca618cd1936449a5420675db672f7a144045fca014c2207fe549cd7e743bcd2076459e69f1cd50dca1
             */

            //get post parameters        
            String routeCount = request.getParameter("numroutes");
            String hostname = request.getParameter("hostname");
            String version = request.getParameter("versionnumber");
            String expday = request.getParameter("expday");
            String expmonth =request.getParameter("expmonth");
            String expyear = request.getParameter("expyear");
            long created = new java.util.Date().getTime();

            String exp = expmonth + "/" + expday + "/" + expyear;        
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
            java.util.Date d = sdf.parse(exp, new ParsePosition(0));
            long expirydate = d.getTime();

            String licKey = hostname + "|" + routeCount + "|" + version + "|" + expirydate + "|" + created;

            //register bouncycastle jce provider
            java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

            //build rsa public key for excryption
            RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
            new BigInteger("96142334799ba0417560828127f1fef7682059b107f828dfbc953cf5840f5d02433303f43f0743bb253a7ac5539ef9c1e9cc1038c3210315d331c8593d977ce70d9790ba98e565fe5158166dc1b36ae6587d8b8c4fded7687cf2e2983dcc98f81b6e79c11282fe65697b6f52844a9700331a151283681a726f85b8483f012e7e32040a4d0d7e6f6d21008afc5263ddd3ab638b7e6d74f68614752cb3a06f1d5dd416b7a46497e082a7da21e4b151edfbb69e1a9bfb7a77e2ffc048e5df76fb4d6855a2ca8a9865ecf8bcd47848fd104f67f91581151ef59b1f9483bdd2d2596484ba3c10ab679fe4c37b41d96a239ed4316079969beec783714e0aba8bdc96a5", 16),
            new BigInteger("10001", 16));

            KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
            PublicKey pubKey = fact.generatePublic(pubKeySpec);

            Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
            c.init(Cipher.ENCRYPT_MODE, pubKey, new SecureRandom());
            byte[] encrypted = c.doFinal(licKey.getBytes());

            BASE64Encoder encoder = new BASE64Encoder();
            String licenseText = encoder.encode(encrypted);

            //System.out.println("License = " + licenseText);
            //System.out.println("len = " + licenseText.length());

            String sql = "insert into BIELicense";
            sql += " (hostName, routeCount, expiration, created, version, LicenseKey)";
            sql += " values ('" + hostname + "'," + routeCount + "," +expirydate + "," + created + "," + version + ",'" + licenseText + "')";

            Class.forName(dbDriver).newInstance();

    //        Connection conn = DriverManager.getConnection("jdbc:mysql://scorpion.wdi.com/jdepons/?user=jdepons&password=green19");
            Connection conn = DriverManager.getConnection(dbURL);

            Statement stmt = conn.createStatement();
            stmt.execute(sql);
            conn.close();             
        
            //write response
            response.setContentType("text/html");
            java.io.PrintWriter out = response.getWriter();        
            out.println("<html><body><form><textarea name='hostkey' rows='15' cols='60'>" + licenseText + "</textarea></form></body></html>");
            out.close();
        }catch (Exception e) {
            try {
                java.io.PrintWriter out = response.getWriter();        
                out.println(e.getMessage());
                out.close();
            }catch(Exception e2) {
                e.printStackTrace();
            }
        }               
    }    
}
